package com.itheima.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.pojo.dto.PageDto;
import com.itheima.admin.service.IUserService;
import com.itheima.pojo.vo.PageVo;
import com.itheima.pojo.vo.UserVo;
import com.itheima.pojo.dto.UserDto;
import com.itheima.pojo.dto.UserRoleDto;
import com.itheima.pojo.pojo.RoleUser;
import com.itheima.pojo.vo.RoleUserVo;
import com.itheima.pojo.vo.UserPageVo;
import com.itheima.admin.mapper.UserMapper;
import com.itheima.pojo.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserVo queryById(String id) {
        User user=userMapper.selectById(id);
        UserVo userDto= new UserVo();

        BeanUtils.copyProperties(user,userDto);
        userDto.setStatus(user.getState());
        return userDto;
    }

    @Override
    public Boolean deleteById(String ids) {
        return userMapper.deleteById(ids);
    }

    @Override
    public Boolean addUser(UserDto userDto, HttpSession session) {
        User user = new User();
        BeanUtils.copyProperties(userDto, user);
        user.setState(userDto.getStatus());
        log.info("{}",user);
        boolean flag=userMapper.addUser(user);
        session.setAttribute("userId",user.getUserId());
        log.info("{}",session.getAttribute("userId"));

        return flag;
    }

    @Override
    public Boolean updateUser(UserDto userDto) {
        User user=new User();

        BeanUtils.copyProperties(userDto,user);
        user.setState(userDto.getStatus());
        return userMapper.updateUser(user);
    }

    @Override
    public Boolean addUserRole(UserRoleDto userRoleDto) {
        List<RoleUser> roleUserVoList = userRoleDto.getRoleIds().stream().map(roleId ->{
            RoleUser roleUser=new RoleUser();
            roleUser.setUserId(userRoleDto.getUserId());
            roleUser.setRoleId(roleId);
            return roleUser;
        }).collect(Collectors.toList());

        RoleUserVo roleUserVo=RoleUserVo.builder()
                            .roleUserVoList(roleUserVoList)
                            .build();
        return userMapper.addUserRole(roleUserVo);
    }

    @Override
    public PageVo<UserPageVo> queryByPage(PageDto pageDto) {

        //开启分页
        PageHelper.startPage(pageDto.getCurrentPage(), pageDto.getPageSize());

        PageInfo<User> page =  new PageInfo<>(userMapper.selectByPage(pageDto.getStatus(),pageDto.getUsername()));

        //List<User>  转成  List<UserPageVo>  流式编程
        List<UserPageVo> list = page.getList().stream().map(
                user -> {
                    UserPageVo userPageVo = new UserPageVo();
                    BeanUtils.copyProperties(user,userPageVo);
                    userPageVo.setStatus(user.getState());
                    return userPageVo;
                }
        ).collect(Collectors.toList());

        return new PageVo<UserPageVo>(list,Long.valueOf(page.getTotal()).intValue());
    }
}